home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
-
- #define CELLSIZE 16
-
- int main(int argc,char *argv[])
- {
-
- short int dict[CELLSIZE][256];
- short int tempcell[CELLSIZE];
- static int dummy[] = {0x00,0x7E,0x42,0x42,0x42,0x42,0x7E,0x00};
- int maxcell,current,found,match,initial,i,j,getout,taken,given,bytecount;
- FILE *FI,*FO;
-
- fprintf(stderr,"\nCharacter ROM Optimizer (C)1999 Chris Covell (ccovell@direct.ca)\n\n");
-
- if((argc!=3) && (argc!=4))
- {
- fprintf(stderr,"Usage: %s infile outfile [-c]\n\n",argv[0]);
- return(1);
- }
-
- if(!(FI=argc<2? stdin:fopen(argv[1],"rb")))
- { fprintf(stderr,"%s: Can't open input file\n\n",argv[0]);return(1); }
- if(!(FO=argc<2? stdout:fopen(argv[2],"wb")))
- { fprintf(stderr,"%s: Can't open output file\n\n",argv[0]);return(1); }
-
- maxcell=0;
- current=0;
- found=0;
- match=1;
- initial=1;
- getout=0;
- taken=0;
- given=0;
- bytecount=0;
-
-
- while((tempcell[0]=fgetc(FI))>=0)
- {
- taken=taken+1;
- found=0;
- current=0;
- for(i=1; i<CELLSIZE; i=i+1)
- {
- tempcell[i]=fgetc(FI);
- taken=taken+1;
- }
-
- if(initial)
- {
- for(i=0; i<CELLSIZE; i=i+1)
- {
- dict[i][0]=tempcell[i];
- fputc(tempcell[i],FO);
- given=given+1;
- tempcell[i]=fgetc(FI);
- taken=taken+1;
- }
- initial=0;
- }
-
-
- while((found==0) && (current<=maxcell) && (!getout))
- {
- for(i=0; ((i<CELLSIZE) && (match==1)); i=i+1)
- {
- if(tempcell[i] != dict[i][current]) match=0;
- }
- if(match==1)
- {
-
- if(strncmp(argv[3],"-c",2))
- for(j=0; j<(CELLSIZE / 8); j=j+1)
- for(i=0; i<(CELLSIZE / 2); i=i+1)
- {
- fputc(dummy[i],FO);
- given=given+1;
- }
- found=1;
- }
- current=current+1;
- match=1;
- if(taken>=(CELLSIZE * 256)) getout=1;
- }
-
- if(found==0)
- {
- for(i=0; i<CELLSIZE; i=i+1)
- {
- dict[i][current] = tempcell[i];
- fputc(tempcell[i],FO);
- given=given+1;
- }
- maxcell=maxcell+1;
- }
- if(getout)
- {
- bytecount=bytecount+taken;
- maxcell=0;
- match=1;
- initial=1;
- current=0;
- found=0;
- getout=0;
- taken=0;
- }
- }
- if(!strncmp(argv[3],"-c",2))
- {
- bytecount=bytecount-given;
- for(i=0; i<bytecount; i=i+1)
- fputc(0x00,FO);
- }
-
-
- fclose(FO);
- fclose(FI);
-
- return(0);
- }